from odoo import api, fields, models


class PlanMaterialRequirementWizard(models.TransientModel):
    _inherit = 'plan.material.requirement.wizard'

    def get_child_semi_product_ids(self, bom_ids):
        bom_line = bom_ids.bom_line_ids.filtered(lambda x: x.product_id.bom_ids)
        product_id = bom_line.mapped('product_id.product_tmpl_id')
        product_ids = product_id
        if product_id.bom_ids:
            product_ids |= self.get_child_semi_product_ids(product_id.bom_ids)
        return product_ids

    def action_plan_material_requirement(self):
        res = super().action_plan_material_requirement()
        product_id = self.product_id.product_tmpl_id
        product_ids = self.product_id.product_tmpl_id
        product_ids |= self.get_child_semi_product_ids(product_id.bom_ids)
        for line in self.line_ids:
            if not line.product_id.product_tmpl_id.is_allow_to_mes:
                product_ids |= line.product_id.product_tmpl_id
        # product_ids.write({'is_allow_to_mes': True})
        for p in product_ids:
            p.is_allow_to_mes = True
        return res
